package com.markyao.utils;

import org.yaml.snakeyaml.Yaml;

import java.util.HashMap;
import java.util.Map;

public class ResovleConfigUtils {
    public static Map<String, Object> resovleProps(String content) {
        Map<String,Object>map=new HashMap<>();
        String[] split = content.split("\n");
        for (String line : split) {
            String[] ms = line.split("=");
            map.put(ms[0],ms[1]);
        }
        return map;
    }

    public static Map<String, Object> resovleYml(String content) {
        Yaml yaml=new Yaml();
        Map<String,Object>map=new HashMap<>();
        Iterable<Object> objects = yaml.loadAll(content);
        for (Object object : objects) {
            asMap("",object,map);
        }
        return map;
    }

    private static void asMap(String key,Object object,Map<String,Object>result) {
        if (!(object instanceof Map)){
            result.put(key,object);
            return;
        }else {
            Map<String, Object> mp = (Map<String, Object>) object;
            for (String k : mp.keySet()) {
                asMap(key.equals("")?k:key+"."+k,mp.get(k),result);
            }
        }
    }

    public static boolean isYml(String dataId) {
        return dataId.endsWith("yml");
    }
}
